package spider.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * 读取配置文件里的租户信息
 *
 * @author 李延念
 * @create 2018-07-05 15:58
 **/
public class TidsUtil {
    private static Logger logger = LogManager.getLogger(TidsUtil.class);
    public static final String defaultTidsPath = "conf/tids.properties";
    private static List<String> tidsCache;
    public static List<String> getTids(){
        if(tidsCache != null){
            logger.info( "cache tids is " + tidsCache );
            return tidsCache;
        }
        File confFile = new File(defaultTidsPath);
        if (confFile.exists()) {
            Properties prop = new Properties();
            try {
                prop.load(new FileInputStream(confFile));
                String tids = prop.getProperty( "tids" );
                if (tids != null && !tids.isEmpty()) {
                    String[] split = tids.split( "," );
                    tidsCache = Arrays.asList(split);
                    logger.info( "load tids is " + tidsCache );
                    return tidsCache;
                }
            } catch (FileNotFoundException e) {
                logger.error("getTids error", e);
            } catch (IOException e) {
                logger.error("getTids error", e);
            }
        }
        return null;
    }
}
